#!/usr/bin/env python
#
# Copyright 2009 Denis Mingulov
#
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from google.appengine.api import memcache
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext.db import polymodel

def getCurrentPerson():
    if not users.get_current_user():
        return None
    query = Person.gql("WHERE user=:curUser", curUser=users.get_current_user())
    person = query.get()
    if not person:
        person = Person(name='No')
        person.put()
    return person

def getActiveModules(person, forceReload=False):
    hashString = "persons_PersonActiveModules_'%s'" % (person.key())
    if not forceReload:
        data = memcache.get( hashString )
        if data is not None:
            return data

#    query = db.GqlQuery("SELECT * FROM Module WHERE person = :1 AND status>0", person.key())
    query = db.GqlQuery("SELECT * FROM Module WHERE person = :1 AND status IN (1,2) ORDER by class, type", person.key())
    modlist = query.fetch(1000);

    if modlist:
        memcache.set(hashString, modlist)
#    else:
#        memcache.set(hashString, 0)
    return modlist

class Person(polymodel.PolyModel):
    name = db.StringProperty(required=True, multiline=False, indexed=True)
    description = db.TextProperty()
    user = db.UserProperty(auto_current_user_add=True, indexed=True)
    status = db.IntegerProperty(default=1, indexed=True)
    dateReg =  db.DateTimeProperty(auto_now_add=True)
    dateChange = db.DateTimeProperty(auto_now=True)

    # points for battle rank
    persBattlePoints  = db.IntegerProperty(default=0)
    persLevel         = db.IntegerProperty(default=0)

    # characteristics
    persConsitution   = db.IntegerProperty(default=0)
    persWeaponLaser   = db.IntegerProperty(default=0)
    persWeaponPlasma  = db.IntegerProperty(default=0)
    persWeaponRocket  = db.IntegerProperty(default=0)
    persWeaponEnergy  = db.IntegerProperty(default=0)
    persWeaponMiner   = db.IntegerProperty(default=0)
    persWeaponGravity = db.IntegerProperty(default=0)
    persReaction      = db.IntegerProperty(default=0)
    persEndurance     = db.IntegerProperty(default=0)
    persObservation   = db.IntegerProperty(default=0)
    persIntuition     = db.IntegerProperty(default=0)
    persEngineer      = db.IntegerProperty(default=0)
    persIntellect     = db.IntegerProperty(default=0)
    persDefensiveSys  = db.IntegerProperty(default=0)
    persJammingSys    = db.IntegerProperty(default=0)
    persBiomodule     = db.IntegerProperty(default=0)
    persWorker        = db.IntegerProperty(default=0)
    persSciPhysic     = db.IntegerProperty(default=0)
    persSciPower      = db.IntegerProperty(default=0)
    persSciMath       = db.IntegerProperty(default=0)

    def addToFighter(self, fighter):
        fighter.persLevel         = self.persLevel
        fighter.persConsitution   = self.persConsitution  
        fighter.persWeaponLaser   = self.persWeaponLaser  
        fighter.persWeaponPlasma  = self.persWeaponPlasma 
        fighter.persWeaponRocket  = self.persWeaponRocket 
        fighter.persWeaponEnergy  = self.persWeaponEnergy 
        fighter.persWeaponMiner   = self.persWeaponMiner  
        fighter.persWeaponGravity = self.persWeaponGravity
        fighter.persReaction      = self.persReaction     
        fighter.persEndurance     = self.persEndurance    
        fighter.persObservation   = self.persObservation  
        fighter.persIntuition     = self.persIntuition    
        fighter.persEngineer      = self.persEngineer     
        fighter.persIntellect     = self.persIntellect    
        fighter.persDefensiveSys  = self.persDefensiveSys 
        fighter.persJammingSys    = self.persJammingSys   
        fighter.persBiomodule     = self.persBiomodule    
        fighter.persWorker        = self.persWorker       
        fighter.persSciPhysic     = self.persSciPhysic    
        fighter.persSciPower      = self.persSciPower     
        fighter.persSciMath       = self.persSciMath      
